import unittest

from mudpyl import matchers
from mudpyl.matchers import alias

class TestRegexMatcher(unittest.TestCase):

    def setUp(self):
        
        self.matcher=matchers.Matcher(regex=True,regexExpr='Test')

    def testMatch(self):

        self.assertTrue(self.matcher.match('Test'))

    def testNotMatch(self):

        self.assertFalse(self.matcher.match('Nontest'))

    def testMatchResult(self):

        res=self.matcher.match('ThisTestisTestisTest')

        self.assertEqual(len(res),3)

class TestWildMatcher(unittest.TestCase):

    def setUp(self):

        def f(x):
            if not isinstance(x,str):
                return False

            if len(x)<3 : return False

            return x[0],x[1],x[2]
                
        self.matcher=matchers.Matcher(regex=False,wild=f )

    def testMatch(self):
        self.assertTrue(self.matcher.match('123'))
    
    def testNonMatch(self):
        self.assertFalse(self.matcher.match(12))

class TestFuncCallMatcher(unittest.TestCase):

    def setUp(self):
        self.matcher=matchers.Matcher(regex=True,regexExpr='Test',func=lambda x,y:'!')

    def testCall(self):
        y=''

        for match in self.matcher.match('TestTestTest'):
            y+=self.matcher(match,None)

        self.assertEqual(y,'!!!')

class TestDecorator(unittest.TestCase):

    def setUp(self):

        @alias('Test')
        def test(matching,realm):
            return matching

        self.tmp=test

    def testCall(self):

        self.assertFalse(self.tmp.match('WTF?'))
        self.assertTrue(self.tmp.match('TTestT'))



if __name__=="__main__":
    unittest.main()
